package com.ylyq.ylxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylyq.ylxx.entity.YlyqUser;
import com.ylyq.ylxx.mapper.YlyqUserMapper;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.service.IYlyqUserService;
import com.ylyq.ylxx.util.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaweichao
 * @since 2020-06-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class YlyqUserServiceImpl extends ServiceImpl<YlyqUserMapper, YlyqUser> implements IYlyqUserService {

    @Override
    public ResponseData register(String username, String password) {

        if (StringUtils.isBlank(username))
            return new ResponseData(ResponseData.Result.FAIL.getCode(), "用户名不能为空");
        if (StringUtils.isBlank(password))
            return new ResponseData(ResponseData.Result.FAIL.getCode(), "密码不能为空");

        QueryWrapper<YlyqUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        int count = count(wrapper);
        if (count > 0) {
            return new ResponseData(ResponseData.Result.FAIL.getCode(), "用户名存在");
        }

        password = MD5Utils.encrypt(password);

        YlyqUser ylyqUser = new YlyqUser();
        ylyqUser.setPassword(password);
        ylyqUser.setUsername(username);
        boolean success = save(ylyqUser);
        if (success) {
            return new ResponseData(ResponseData.Result.SUCCESS);
        } else {
            return new ResponseData(ResponseData.Result.FAIL);
        }

    }
}
